# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exporters import CsvItemExporter

from scrapy.pipelines.images import ImagesPipeline

from scrapy.http import Request
from .settings import IMAGES_STORE
import os

# 使用scrapy 下载图片 imagesPipeline
class DouyuImagePipline(ImagesPipeline):
    def get_media_requests(self, item, info):
        # 1.图片url item['']
        image_url = item['vertical_src']
        # 2.发图片请求--engine--sheduler--engine-
        yield Request(image_url)

    def item_completed(self, results, item, info):
        # 更换 图片的名字 昵称
        # 1. 老的 图片路径
        old_path = IMAGES_STORE + [x['path'] for ok, x in results if ok][0]
        # 2. 新的 图片路径
        new_path = IMAGES_STORE + item['nickname'] + '.jpg'
        # 3. os替换rename()
        # 循环之后 因为 没有上传头像的用户--占位图 地址一样 倒置, 后面的 用户的 无法替换
        try:
            os.rename(old_path, new_path)

        except Exception as e:
            print('完毕')

        return item








# CSV 文件管道
class DouyuPipeline(object):
    def open_spider(self, spider):
        self.file = open('douyu.csv', 'wb')
        self.writer = CsvItemExporter(self.file)
        self.writer.start_exporting()

    def process_item(self, item, spider):
        self.writer.export_item(item)
        return item

    def close_spider(self, spider):
        self.writer.finish_exporting()
        self.file.close()

# 文件管道  普通格式
# class DouyuPipeline(object):
#     def open_spider(self, spider):
#         self.file = open('01.txt', 'w')
#
#     def process_item(self, item, spider):
#
#         dict_data = dict(item)
#         str_data = json.dumps(dict_data) + '\n'
#         self.file.write(str_data)
#         return item
#
#     # 关闭写入
#     def close_spider(self, spider):
#         self.file.close()